//
//  LoginAndRegisterViewController.m
//  Comma
//
//  Created by liuchang on 15/1/16.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LoginAndRegisterViewController.h"
#import "CollegeRegisterViewController.h"
#import "URLMacro.h"
#import "GlobalMethod.h"
#import "AFNetWorkingYU.h"

/*注册页面右按钮的frame*/
#define REGIST_RIGHT_X _registView.rightUpButton.frame.origin.x
#define REGIST_RIGHT_Y _registView.rightUpButton.frame.origin.y
#define REGIST_RIGHT_WIDTH _registView.rightUpButton.frame.size.width
#define REGIST_RIGHT_HEIGHT _registView.rightUpButton.frame.size.height

/*登录页面右按钮的frame*/
#define LOGIN_RIGHT_X _loginView.rightUpButton.frame.origin.x
#define LOGIN_RIGHT_Y _loginView.rightUpButton.frame.origin.y
#define LOGIN_RIGHT_WIDTH _loginView.rightUpButton.frame.size.width
#define LOGIN_RIGHT_HEIGHT _loginView.rightUpButton.frame.size.height

/*注册页面下方按钮的frame*/
#define REGIST_DOWN_X _registView.centerDownButton.frame.origin.x
#define REGIST_DOWN_Y _registView.centerDownButton.frame.origin.y
#define REGIST_DOWN_WIDTH _registView.centerDownButton.frame.size.width
#define REGIST_DOWN_HEIGHT _registView.centerDownButton.frame.size.height

/*登录页面下方按钮的frame*/
#define LOGIN_DOWN_X _loginView.centerDownButton.frame.origin.x
#define LOGIN_DOWN_Y _loginView.centerDownButton.frame.origin.y
#define LOGIN_DOWN_WIDTH _loginView.centerDownButton.frame.size.width
#define LOGIN_DOWN_HEIGHT _loginView.centerDownButton.frame.size.height

/*注册身份页面的左按钮的frame*/
#define IDENTITY_LEFT_X _identity.leftUpButton.frame.origin.x
#define IDENTITY_LEFT_Y _identity.leftUpButton.frame.origin.y
#define IDENTITY_LEFT_WIDTH _identity.leftUpButton.frame.size.width
#define IDENTITY_LEFT_HEIGHT _identity.leftUpButton.frame.size.height

/*注册身份页面的下方进入按钮的frame*/
#define IDENTITY_DOWN_X _identity.centerDownButton.frame.origin.x
#define IDENTITY_DOWN_Y _identity.centerDownButton.frame.origin.y
#define IDENTITY_DOWN_WIDTH _identity.centerDownButton.frame.size.width
#define IDENTITY_DOWN_HEIGHT _identity.centerDownButton.frame.size.height
@interface LoginAndRegisterViewController ()

{
    NSInteger _timeCount;
    NSTimer  *_timer;
}

@end

@implementation LoginAndRegisterViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getIdentityType:) name:@"IDENTITY" object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 主视图
    self.mainView = [[MainView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self createLoginAndRegister];
    [self createLoginView];
    [self createRegistView];
    [self createIdentityRegistView];
    [self createCollegeRegist];
    [self.mainView bringSubviewToFront:_loginAndRegister];
    self.view = self.mainView;
        
}

#pragma mark - 创建login&register视图
- (void)createLoginAndRegister
{
    self.loginAndRegister = [[LoginAndRegister alloc] initWithLeftUpButton:nil rightUpButton:nil upTextField:nil centerTextField:nil downTextField:nil upButton:nil centerButton:nil downButton:nil centerDownButton:nil];
    self.loginAndRegister.frame = CGRectMake(0, 312, SCREEN_WIDTH, SCREEN_HEIGHT - 312);
    [_loginAndRegister createLoginButton];
    [_loginAndRegister createRegisterButton];
    
    [_loginAndRegister.registButton addTarget:self action:@selector(registAndLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [_loginAndRegister.loginButton addTarget:self action:@selector(registAndLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:_loginAndRegister];
    
    
    
}

#pragma mark - 创建login视图
- (void)createLoginView
{
    self.loginView = [[LoginView alloc] initWithLeftUpButton:nil rightUpButton:@"注册 >" upTextField:@"邮箱/手机号" centerTextField:@"密码" downTextField:nil upButton:nil centerButton:@"忘记密码?" downButton:nil centerDownButton:@"登录"];
    // 自定义控件的frame
    [self.loginView changeControlType];
    
    // 为跳转注册按钮添加点击事件
    [_loginView.rightUpButton addTarget:self action:@selector(registButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 为登录按钮添加点击事件
    [_loginView.centerDownButton addTarget:self action:@selector(loginEnterAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加视图到主视图上的右侧
    [self.mainView addSubview:_loginView];
}

#pragma mark - 创建register视图
- (void)createRegistView
{
    self.registView = [[RegisterView alloc] initWithLeftUpButton:nil rightUpButton:@"登录 >" upTextField:@"手机号" centerTextField:@"密  码" downTextField:@"验证码" upButton:nil centerButton:nil downButton:@"发送验证码 >" centerDownButton:@"下一步"];
    
    // 手动创建密码明示按钮
    [_registView createCenterButtonWithTitle:nil];
    
    // 自定义控件的frame
    [_registView changeControlType];
    
    // 为跳转登录按钮添加点击事件
    [_registView.rightUpButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 下一步按钮点击事件
    [_registView.centerDownButton addTarget:self action:@selector(nextPageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 发送验证码按钮点击事件
    [_registView.downButton addTarget:self action:@selector(verifyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加视图到主视图上的右侧
    [self.mainView addSubview:_registView];
}

#pragma mark - 创建选择身份页面
- (void)createIdentityRegistView
{
    self.identity = [[IdentityRegisterView alloc] initWithLeftUpButton:@"< 上一步" rightUpButton:nil upTextField:@"学  校" centerTextField:@"姓  名" downTextField:nil upButton:nil centerButton:nil downButton:nil centerDownButton:@"进入"];    
    // 创建学生类型, 性别
    [_identity createStudentAndSexButton];
    _identity.upTextField.delegate = self;
    // 为 "上一步" 回到注册界面按钮添加点击事件
    [_identity.leftUpButton addTarget:self action:@selector(lastPageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 为 "进入" 按钮添加点击事件
    [_identity.centerDownButton addTarget:self action:@selector(registUserAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 将整个界面放到主视图的右侧
    [self.mainView addSubview:_identity];
    
}

#pragma mark - 创建学校选择页面
- (void)createCollegeRegist
{
    self.collegeRegist = [[CollegeRegisterViewController alloc] init];
    self.collegeRegist.delegate = self;
}

#pragma mark - 首页登录&注册选择按钮点击事件
- (void)registAndLoginAction:(UIButton *)button
{
    // 不管点击注册还是登录按钮, 视图向左移动
    [self beginAnimationWithView:self.loginAndRegister.registButton delay:0 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:self.loginAndRegister.loginButton delay:0.03 distance:-SCREEN_WIDTH];
    if (button.tag == 1) {
        [self.mainView bringSubviewToFront:_registView];
        // 点击了注册按钮, 注册界面飞出来
        _registView.rightUpButton.frame = CGRectMake(REGIST_RIGHT_X - SCREEN_WIDTH, REGIST_RIGHT_Y, REGIST_RIGHT_WIDTH, REGIST_RIGHT_HEIGHT);
        [self beginAnimationWithView:_registView.upTextField delay:0 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.upLine delay:0.03 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.centerTextField delay:0.06 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downLine delay:0.09 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downTextField delay:0.12 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.centerButton delay:0.06 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downButton delay:0.15 distance:-SCREEN_WIDTH];
        self.registView.centerDownButton.frame = CGRectMake(REGIST_DOWN_X - SCREEN_WIDTH, REGIST_DOWN_Y, REGIST_DOWN_WIDTH, REGIST_DOWN_HEIGHT); 
        
    } else if (button.tag == 2) {
        [self.mainView bringSubviewToFront:_loginView];
        
        // 点击登录, 各控件移动动画
        _loginView.rightUpButton.frame = CGRectMake(LOGIN_RIGHT_X - SCREEN_WIDTH, LOGIN_RIGHT_Y, LOGIN_RIGHT_WIDTH, LOGIN_RIGHT_HEIGHT);
        [self beginAnimationWithView:_loginView.upTextField delay:0 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_loginView.lineView delay:0.03 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_loginView.centerTextField delay:0.06 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_loginView.centerButton delay:0.09 distance:-SCREEN_WIDTH];
        self.loginView.centerDownButton.frame = CGRectMake(LOGIN_DOWN_X - SCREEN_WIDTH, LOGIN_DOWN_Y, LOGIN_DOWN_WIDTH, LOGIN_DOWN_HEIGHT);
        
    }
}

#pragma mark - 点击注册切换到注册页面
- (void)registButtonAction:(UIButton *)button
{
    [self.mainView bringSubviewToFront:_registView];
    // 设置将要出来的控件的原始位置
    _registView.rightUpButton.frame = CGRectMake(SCREEN_WIDTH - 57 + SCREEN_WIDTH, 0, 41, 44);
    _registView.upTextField.frame = CGRectMake(16 + SCREEN_WIDTH, 44, SCREEN_WIDTH - 110, 44);
    _registView.upLine.frame = CGRectMake(SCREEN_WIDTH, 88, SCREEN_WIDTH, 0.5);
    _registView.centerTextField.frame = CGRectMake(16 + SCREEN_WIDTH, 88, SCREEN_WIDTH - 110, 44);
    _registView.downLine.frame = CGRectMake(SCREEN_WIDTH, 132, SCREEN_WIDTH, 0.5);
    _registView.downTextField.frame = CGRectMake(16 + SCREEN_WIDTH, 132, SCREEN_WIDTH - 110, 44);
    _registView.centerButton.frame = CGRectMake(SCREEN_WIDTH - 41 + SCREEN_WIDTH, 100, 25, 16);
    _registView.downButton.frame = CGRectMake(SCREEN_WIDTH - 99 + SCREEN_WIDTH, 132, 83, 44);
    _registView.centerDownButton.frame = CGRectMake(SCREEN_WIDTH / 2 - 50 + SCREEN_WIDTH, 204, 100, 44);

    // 登录页面的控件向左出去
    _loginView.rightUpButton.frame = CGRectMake(LOGIN_RIGHT_X - SCREEN_WIDTH, LOGIN_RIGHT_Y, LOGIN_RIGHT_WIDTH, LOGIN_RIGHT_HEIGHT);
    [self beginAnimationWithView:_loginView.upTextField delay:0 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.lineView delay:0.03 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.centerTextField delay:0.06 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.centerButton delay:0.09 distance:-SCREEN_WIDTH];
    self.loginView.centerDownButton.frame = CGRectMake(LOGIN_DOWN_X - SCREEN_WIDTH, LOGIN_DOWN_Y, LOGIN_DOWN_WIDTH, LOGIN_DOWN_HEIGHT);   
    
    // 注册页面的控件出现
    _registView.rightUpButton.frame = CGRectMake(REGIST_RIGHT_X - SCREEN_WIDTH, REGIST_RIGHT_Y, REGIST_RIGHT_WIDTH, REGIST_RIGHT_HEIGHT);
    [self beginAnimationWithView:_registView.upTextField delay:0.3 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.upLine delay:0.33 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerTextField delay:0.36 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downLine delay:0.39 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downTextField delay:0.42 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerButton delay:0.36 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downButton delay:0.45 distance:-SCREEN_WIDTH];
    self.registView.centerDownButton.frame = CGRectMake(REGIST_DOWN_X - SCREEN_WIDTH, REGIST_DOWN_Y, REGIST_DOWN_WIDTH, REGIST_DOWN_HEIGHT);   
  
}

#pragma mark - 点击登录切换到登录界面
- (void)loginButtonAction:(UIButton *)button
{
    [self.mainView bringSubviewToFront:_loginView];
    _loginView.rightUpButton.frame = CGRectMake(SCREEN_WIDTH - 57 + SCREEN_WIDTH, 0, 41, 44);
    _loginView.upTextField.frame = CGRectMake(16 + SCREEN_WIDTH, 44, SCREEN_WIDTH - 110, 44);
    _loginView.lineView.frame = CGRectMake(SCREEN_WIDTH, 88, SCREEN_WIDTH, 0.5);
    _loginView.centerTextField.frame = CGRectMake(16 + SCREEN_WIDTH, 88, SCREEN_WIDTH - 110, 44);
    _loginView.centerButton.frame = CGRectMake(SCREEN_WIDTH - 80 + SCREEN_WIDTH, 88, 65, 44);
    _loginView.centerDownButton.frame = CGRectMake(SCREEN_WIDTH / 2 - 50 + SCREEN_WIDTH, 204, 100, 44);
    
    // 注册界面的控件向左出去
    _registView.rightUpButton.frame = CGRectMake(REGIST_RIGHT_X - SCREEN_WIDTH, REGIST_RIGHT_Y, REGIST_RIGHT_WIDTH, REGIST_RIGHT_HEIGHT);
    [self beginAnimationWithView:_registView.upTextField delay:0 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.upLine delay:0.03 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerTextField delay:0.06 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downLine delay:0.09 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downTextField delay:0.12 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerButton delay:0.06 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downButton delay:0.15 distance:-SCREEN_WIDTH];
    self.registView.centerDownButton.frame = CGRectMake(REGIST_DOWN_X - SCREEN_WIDTH, REGIST_DOWN_Y, REGIST_DOWN_WIDTH, REGIST_DOWN_HEIGHT);   
    
    // 登录界面的控件显示
    _loginView.rightUpButton.frame = CGRectMake(LOGIN_RIGHT_X - SCREEN_WIDTH, LOGIN_RIGHT_Y, LOGIN_RIGHT_WIDTH, LOGIN_RIGHT_HEIGHT);
    [self beginAnimationWithView:_loginView.upTextField delay:0.3 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.lineView delay:0.33 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.centerTextField delay:0.36 distance:-SCREEN_WIDTH];
    [self beginAnimationWithView:_loginView.centerButton delay:0.39 distance:-SCREEN_WIDTH];
    self.loginView.centerDownButton.frame = CGRectMake(LOGIN_DOWN_X - SCREEN_WIDTH, LOGIN_DOWN_Y, LOGIN_DOWN_WIDTH, LOGIN_DOWN_HEIGHT);   
    
}

#pragma mark - 点击下一步进入注册身份页面
- (void)nextPageButtonAction:(UIButton *)button
{   
    // 点击下一步, 先获取输入框的内容
    self.phoneNumber = self.registView.upTextField.text;
    self.password = self.registView.centerTextField.text;
    self.verifyCode = self.registView.downTextField.text;
    
    // 验证输入框输入内容是否符合要求
#warning ------------------------
    BOOL verify = [self verifyRegistMessage];
    if (verify) {
        [self.mainView bringSubviewToFront:_identity];
        // 当前 "注册页面和logo图片" 向左出去, 上传头像按钮显示
        [self beginAnimationWithView:_mainView.imgView delay:0 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_mainView.photoButton delay:0 distance:-SCREEN_WIDTH];
        
        // 注册页面的控件向左飞出去
        _registView.rightUpButton.frame = CGRectMake(REGIST_RIGHT_X - SCREEN_WIDTH, REGIST_RIGHT_Y, REGIST_RIGHT_WIDTH, REGIST_RIGHT_HEIGHT);
        [self beginAnimationWithView:_registView.upTextField delay:0 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.upLine delay:0.03 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.centerTextField delay:0.06 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downLine delay:0.09 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downTextField delay:0.12 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.centerButton delay:0.06 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_registView.downButton delay:0.15 distance:-SCREEN_WIDTH];
        self.registView.centerDownButton.frame = CGRectMake(REGIST_DOWN_X - SCREEN_WIDTH, REGIST_DOWN_Y, REGIST_DOWN_WIDTH, REGIST_DOWN_HEIGHT); 
        
        // 注册身份页面的控件逐个儿飞出来
        self.identity.leftUpButton.frame = CGRectMake(IDENTITY_LEFT_X - SCREEN_WIDTH, IDENTITY_LEFT_Y, IDENTITY_LEFT_WIDTH, IDENTITY_LEFT_HEIGHT);
        [self beginAnimationWithView:_identity.upTextField delay:0.3 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_identity.upLine delay:0.33 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_identity.centerTextField delay:0.36 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_identity.stuSuperView delay:0.39 distance:-SCREEN_WIDTH];
        [self beginAnimationWithView:_identity.sexSuperView delay:0.42 distance:-SCREEN_WIDTH];
        self.identity.centerDownButton.frame = CGRectMake(IDENTITY_DOWN_X - SCREEN_WIDTH, IDENTITY_DOWN_Y, IDENTITY_DOWN_WIDTH, IDENTITY_DOWN_HEIGHT);
    }
    
    
}

#pragma mark - 点击上一步进入注册页面
- (void)lastPageButtonAction:(UIButton *)button
{
    [self.mainView endEditing:YES];
    [self.mainView bringSubviewToFront:_registView];
    // 点击上一步, 选择头像按钮 向右出去, logo图片显示到屏幕上
    [self beginAnimationWithView:_mainView.photoButton delay:0 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_mainView.imgView delay:0 distance:SCREEN_WIDTH];

    // 注册身份页面的控件飞出去
    self.identity.leftUpButton.frame = CGRectMake(IDENTITY_LEFT_X + SCREEN_WIDTH, IDENTITY_LEFT_Y, IDENTITY_LEFT_WIDTH, IDENTITY_LEFT_HEIGHT);
    [self beginAnimationWithView:_identity.upTextField delay:0 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_identity.upLine delay:0.03 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_identity.centerTextField delay:0.06 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_identity.sexSuperView delay:0.09 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_identity.stuSuperView delay:0.12 distance:SCREEN_WIDTH];
    self.identity.centerDownButton.frame = CGRectMake(IDENTITY_DOWN_X + SCREEN_WIDTH, IDENTITY_DOWN_Y, IDENTITY_DOWN_WIDTH, IDENTITY_DOWN_HEIGHT);
    
    // 注册页面的控件显示出来
    _registView.rightUpButton.frame = CGRectMake(REGIST_RIGHT_X + SCREEN_WIDTH, REGIST_RIGHT_Y, REGIST_RIGHT_WIDTH, REGIST_RIGHT_HEIGHT);
    [self beginAnimationWithView:_registView.upTextField delay:0 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.upLine delay:0.33 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerTextField delay:0.36 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downLine delay:0.39 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downTextField delay:0.42 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.centerButton delay:0.36 distance:SCREEN_WIDTH];
    [self beginAnimationWithView:_registView.downButton delay:0.42 distance:SCREEN_WIDTH];
    self.registView.centerDownButton.frame = CGRectMake(REGIST_DOWN_X + SCREEN_WIDTH, REGIST_DOWN_Y, REGIST_DOWN_WIDTH, REGIST_DOWN_HEIGHT);
    
}

#pragma mark - 键盘开始编辑时弹出选择大学视图
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField.placeholder isEqualToString:@"学  校"]) {

        [self presentViewController:self.collegeRegist animated:YES completion:nil];
    }
}

#pragma mark - 通过协议传值得到选中的大学
- (void)assignCollegeName:(NSString *)name
{
    self.identity.upTextField.text = name;
}

#pragma mark - 进行注册的点击事件
- (void)registUserAction:(UIButton *)button
{
    self.collegeName = _identity.upTextField.text;
    self.name = _identity.centerTextField.text;
    BOOL verify = [self verifyIdentityMessage];
    if (verify) {
        // 如果所有信息都符合要求, 进行注册请求
        NSMutableDictionary *parmeters = [self getParmeterDic];
        [parmeters setObject:_password forKey:@"password"];
        [parmeters setObject:_verifyCode forKey:@"verifyCode"];
        [parmeters setObject:@"60" forKey:@"validTime"];
        [parmeters setObject:@"true" forKey:@"rememberPassword"];    
        NSString *body = [self getFullParmeterWithDic:parmeters];
        [AFNetWorkingYU netWorkWithURL:@"http://www.isu.so/user/mobi_reg" body:body resultBlock:^(id result) {
            NSDictionary *dic = (NSDictionary *)result;
            NSString *msg = [dic objectForKey:@"msg"];
            msg = [msg stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [self showAlertWithMessage:msg];
        }];
    }
    
}

#pragma mark - 登录点击事件
- (void)loginEnterAction:(UIButton *)button
{
    // 获取手机号和密码
    self.phoneNumber = self.loginView.upTextField.text;
    self.password = self.loginView.centerTextField.text;
    // 验证输入是否有误
    BOOL verify = [self verifyLoginMessage];
    if (verify) {
        NSString *approveKey = [self getApproveKey];
        NSString *deviceParam = [GlobalMethod getApproveKey];
        NSMutableDictionary *parmeters = [NSMutableDictionary dictionary];
        [parmeters setObject:_phoneNumber forKey:@"mobile"];
        [parmeters setObject:_password forKey:@"password"];
        [parmeters setObject:approveKey forKey:@"approveKey"];
        [parmeters setObject:deviceParam forKey:@"deviceParam"];
        [parmeters setObject:@"60" forKey:@"validTime"];
        [parmeters setObject:@"true" forKey:@"rememberPassword"];    
        NSString *body = [self getFullParmeterWithDic:parmeters];
        [AFNetWorkingYU netWorkWithURL:@"http://www.isu.so/user/mobi_login" body:body resultBlock:^(id result) {
            NSDictionary *dic = (NSDictionary *)result;
            NSString *msg = [dic objectForKey:@"msg"];
            msg = [msg stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [self showAlertWithMessage:msg];
        }];
    }

}

#pragma mark - 获取验证码点击事件
- (void)verifyButtonAction:(UIButton *)button
{
    
    // 获得deviceID, usingKey
    NSString *deviceID = [GlobalMethod GetIOSUUID];
    NSString *usingKey = [GlobalMethod getApproveKey];
    
    // 将参数存入字典
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionary];
    self.phoneNumber = _registView.upTextField.text;
    [parmeters setObject:_phoneNumber forKey:@"mobile"];
    [parmeters setObject:deviceID forKey:@"deviceID"];
    [parmeters setObject:usingKey forKey:@"usingKey"];
    // 获得拼接后的参数字符串
    NSString *body = [self getFullParmeterWithDic:parmeters];
    // 发送请求
    [AFNetWorkingYU netWorkWithURL:@"http://www.isu.so/sms_api/req" body:body resultBlock:^(id result) {
        NSDictionary *resultDic = (NSDictionary *)result;
        NSString *msg = [resultDic objectForKey:@"msg"];
        if ([msg isEqualToString:@"success"]) {
            // 倒计时
            _timeCount = 45;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTime) userInfo:nil repeats:YES];
        } else {
            [self showAlertWithMessage:msg];

        }
        
    }];
}

#pragma mark - 封装参数
- (NSString *)getFullParmeterWithDic:(NSDictionary *)dic
{
    NSString *str = @"";
    for (NSString *key in dic) {
        if ([str length] == 0) {
            str = [NSString stringWithFormat:@"%@=%@", key, [dic objectForKey:key]];
        } else {
            str = [NSString stringWithFormat:@"%@&%@=%@", str, key, [dic objectForKey:key]];
        }
    }
    return str;
    
}

#pragma mark - 将主要参数封装到字典中, 用于post请求body的拼接
- (NSMutableDictionary *)getParmeterDic
{
    // 获得approveKey, deviceParam
    NSString *deviceID = [GlobalMethod GetIOSUUID];
    NSString *usingKey = [GlobalMethod getApproveKey];
    
    // 将参数存入字典
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionary];  
    [parmeters setObject:_phoneNumber forKey:@"mobile"];
    [parmeters setObject:deviceID forKey:@"deviceID"];
    [parmeters setObject:usingKey forKey:@"usingKey"];
    
    return parmeters;
}

#pragma mark - 获得approveKey
- (NSString *)getApproveKey
{
    NSString *approveKey = [GlobalMethod GetIOSUUID];
    // 获得NSDate
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYYMMDDhhmmssSSS"];
    NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    approveKey = [NSString stringWithFormat:@"%@%@", approveKey, nowDate];
    approveKey = [GlobalMethod md5:approveKey];
    NSLog(@"%@", approveKey);
    return approveKey;
}

#pragma mark - 动画切换效果
- (void)beginAnimationWithView:(UIView *)view delay:(CGFloat)delay distance:(CGFloat)distance
{
    [UIView beginAnimations:@"lxb" context:nil];
    [UIView setAnimationDelay:delay];
    [UIView setAnimationDuration:0.3];
    view.frame = CGRectMake(view.frame.origin.x + distance, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    [UIView commitAnimations];
}

#pragma mark - 验证输入信息是否符合注册规定
- (BOOL)verifyRegistMessage
{
    if ([_phoneNumber isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入手机号"];
        return NO;
    }
    if (_phoneNumber.length != 11) {
        [self showAlertWithMessage:@"请输入11位手机号"];
        return NO;
    }
    if ([_password isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入密码"];
        return NO;
    }
    if (_password.length < 6) {
        [self showAlertWithMessage:@"密码不能低于6位"];
        return NO;
    }
    if ([_verifyCode isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入验证码"];
        return NO;
    }
    return YES;
}

#pragma mark - 验证输入信息是否符合注册规定
- (BOOL)verifyIdentityMessage
{
    if ([_collegeName isEqualToString:@""]) {
        [self showAlertWithMessage:@"请选择学校"];
        return NO;
    }
    if ([_name isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入姓名"];
        return NO;
    }
    return YES;
}

#pragma mark - 验证输入信息是否符合登录规定
- (BOOL)verifyLoginMessage
{
    if ([_phoneNumber isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入手机号"];
        return NO;
    }
    if (_phoneNumber.length != 11) {
        [self showAlertWithMessage:@"请输入11位手机号"];
        return NO;
    }
    if ([_password isEqualToString:@""]) {
        [self showAlertWithMessage:@"请输入密码"];
        return NO;
    }
    return YES;
}

#pragma mark - 提示框
- (void)showAlertWithMessage:(NSString *)message
{
    self.alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [_alert show];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dismissAlert) userInfo:nil repeats:NO];
}

- (void)dismissAlert
{
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}

#pragma mark - 点击选择身份类型的时候得到选中的title
- (void)getIdentityType:(NSNotification *)notify
{
    NSString *type = [[notify userInfo] objectForKey:@"type"];
    NSLog(@"%@", type);
}


#pragma mark - 
- (void)countDownTime
{
    [self.registView.downButton setTitle:[NSString stringWithFormat:@"%ld", _timeCount] forState:UIControlStateNormal];
    if (_timeCount == 0) {
        [_timer invalidate];
        [self.registView.downButton setTitle:@"重新发送 >" forState:UIControlStateNormal];
    }
    _timeCount--;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
